package models

import "github.com/jinzhu/gorm"

type AppEnv struct {
	AppId int `json:"app_id"`
	AppName string `json:"app_name"`
	AppNamespace string `json:"app_namespace"`
	AppDomain string `json:"app_domain"`
	AppLinuxHost string `json:"app_linux_host"`
	AppLinuxPort int `json:"app_linux_port"`
	AppLinuxUser string `json:"app_linux_user"`
	AppLinuxPassword string `json:"app_linux_password"`
	AppMysqlHost string `json:"app_mysql_host"`
	AppMysqlPort int `json:"app_mysql_port"`
	AppMysqlDB string `json:"app_mysql_db"`
	AppMysqlUser string `json:"app_mysql_user"`
	AppMysqlPassword string `json:"app_mysql_password"`
	AppRedisHost string `json:"app_redis_host"`
	AppRedisPort int `json:"app_redis_port"`
	AppRedisDB int `json:"app_redis_db"`
	AppRedisPassword string `json:"app_redis_password"`
	AppZookeeperHost string
	AppZookeeperPort int
	AppZookeeperPassword string
}

// GetAppEnvById 根据应用ID返回app所有环境信息
func GetAppEnvById(appId int, appNamespace string) (*AppEnv, error){
	var appEnv AppEnv
	err := db.Where("app_id = ? and app_namespace = ?", appId, appNamespace).Find(&appEnv).Error
	if err != gorm.ErrRecordNotFound && err != nil {
		return nil, err
	}
	if err == gorm.ErrRecordNotFound {
		return nil, nil
	}
	return &appEnv, nil
}

// AppSelects 返回所有应用列表
func AppEnvSelects(appId int) ([]*AppEnv, error){
	var (
		env []*AppEnv
		envs []*AppEnv
	)

	err := db.Where("app_id = ?", appId).Find(&envs).Error
	if err != gorm.ErrRecordNotFound && err != nil {
		return nil, err
	}

	for _, v := range envs {
		env = append(env, v)
	}

	return env, nil
}